package cn.javadog.algorithm.e1_100;


/**
 * 报数
 * https://leetcode-cn.com/problems/count-and-say/
 * @author Sprite
 */
public class Algorithm38 {
	public static void main(String[] args) {
		System.out.println(countAndSay(1));
	}

	public static String countAndSay(int n) {
		StringBuilder sb = new StringBuilder("1");
		for (int i = 1; i < n; i++) {
			char[] chars = sb.toString().toCharArray();
			sb = new StringBuilder();
			int k = 0;
			for (int j = 0; j < chars.length; j++) {
				if (chars[j] != chars[k]){
					sb.append(j-k);
					sb.append(chars[k]);
					k = j;
				}
			}
			sb.append(chars.length-k);
			sb.append(chars[k]);
		}
		return sb.toString();
	}
}
